﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Excel;
using System.Configuration;
using System.IO;
using System.Data;

/// <summary>
/// Summary description for LanguageManage
/// </summary>
public class LanguageManage
{
    private static readonly String name = "_CurrentLanguage";
    public String currentLanguageID = "en";
    private LanguageChangeManagement languageManagement;

    public LanguageManage()
    {
        try
        {
            if (HttpContext.Current.Session[name] != null)
            {
                currentLanguageID = HttpContext.Current.Session[name].ToString();
            }
            else
            {
                Reset();
            }
        }
        catch { }
        languageManagement = new LanguageChangeManagement();
    }

    public String this[String key]
    {
        get { return languageManagement.Get(currentLanguageID, key); }
    }


    public String Get(String key)
    {
        return languageManagement.Get(currentLanguageID, key);
    }

    public bool ChangeLanguage(String langguageID)
    {
        if (languageManagement.IsSupportLangauge(langguageID))
        {
            this.currentLanguageID = langguageID;
            HttpContext.Current.Session[name] = currentLanguageID;
            return true;
        }
        return false;
    }

    public string GetLanguage()
    {
        return currentLanguageID;
    }

    private void Reset()
    {
        currentLanguageID = ConfigurationSettings.AppSettings["DefaultLanguage"];
        HttpContext.Current.Session[name] = currentLanguageID;
    }


    class LanguageChangeManagement
    {
        private static String name = "_LanguageManagement";
        public static Object Lock = new object();
        Dictionary<String, LanguageItemSet> languages;



        public LanguageChangeManagement()
        {
            try
            {
                if (HttpContext.Current.Application[name] != null)
                {
                    languages = HttpContext.Current.Application[name] as Dictionary<string, LanguageItemSet>;
                }
                else
                {
                    Reset();
                }
            }
            catch { Reset(); }
        }

        private void Reset()
        {
            lock (Lock)
            {
                if (languages != null) return;

                languages = new Dictionary<string, LanguageItemSet>();

                IExcelDataReader excelReader;
                String filePath = ConfigurationSettings.AppSettings["LanguageFilePath"];
                filePath = System.Web.HttpContext.Current.Server.MapPath(filePath);
                FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
                excelReader = filePath.EndsWith("xlsx")
                                  ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                                  : ExcelReaderFactory.CreateBinaryReader(stream);


                excelReader.IsFirstRowAsColumnNames = true;
                DataTable result = excelReader.AsDataSet().Tables[0];

                List<String> listLanguageId = new List<string>();

                for (int i = 1; i < result.Columns.Count; i++)
                {
                    DataColumn col = result.Columns[i];
                    listLanguageId.Add(col.Caption);
                    languages.Add(col.Caption, new LanguageItemSet());
                }

                foreach (DataRow row in result.Rows)
                {
                    for (int i = 0; i < listLanguageId.Count; i++)
                    {
                        languages[listLanguageId[i]].Insert(row[0].ToString(), row[i + 1].ToString());
                    }
                }

                HttpContext.Current.Application[name] = languages;
            }
        }

        public String Get(String languageId, String key)
        {
            try
            {
                return languages[languageId].Get(key);
            }
            catch
            {
                return null;
            }
        }

        public bool IsSupportLangauge(String languageId)
        {
            try
            {
                if (languages[languageId] != null)
                    return true;
            }
            catch { }
            return false;
        }
    }

    class LanguageItemSet
    {
        public String id { get; set; }
        Dictionary<String, String> dicLanguageItem = new Dictionary<string, string>();

        public bool Insert(String key, String value)
        {
            try
            {
                dicLanguageItem.Add(key, value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public String Get(String key)
        {
            try
            {
                return dicLanguageItem[key];
            }
            catch
            {
                return null;
            }
        }
    }
}